﻿using System.Diagnostics;
using System.Transactions;
using BrettM.Security;

namespace BrettM.Commands
{
    public abstract class CommandTransaction<T> 
        : Command<T>
    {
        protected CommandTransaction(ISessionContext context) 
            : base(context)
        {
        }

        protected CommandTransaction(ISessionContext context, WarningSuppressions warningSupressions)  
            : base(context, warningSupressions)
        {
        }

        internal override void InternalExecute(ExecuteResult<T> er)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                // Execute the command
                base.InternalExecute(er);

                // Commite the transaction if the result is either a
                // Success or Warning.  
                if (er.Status != ExecuteResultStatus.Failure)
                {
                    Trace.WriteLine(string.Format("Cammand.Execute {0}", this.GetType().ToString()));
                    scope.Complete();
                }
            }

        }
    }
}
